package com.ict.txmsv2.services;

import cn.hutool.core.collection.CollectionUtil;
import com.ict.txmsv2.mapper.SumTimeMapper;
import com.ict.txmsv2.mapper.UserMapper;
import com.ict.txmsv2.model.ClockInRecord;
import com.ict.txmsv2.model.RespPageBean;
import com.ict.txmsv2.model.SumTimeVo;
import com.ict.txmsv2.utils.DateUtils;
import com.ict.txmsv2.utils.SdfBuilder;
import com.ict.txmsv2.model.SumTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author yoleen
 * @date 2020/12/16 11:55
 */

@Service
public class SumtimeService {
    @Autowired
    SumTimeMapper sumTimeMapper;
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;


    public List<SumTime> getSumTimesForWeek(int cnt) {
        Calendar c = Calendar.getInstance();
        //设置每周第一天为星期一
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.add(Calendar.DAY_OF_YEAR,  7 * cnt);
        String begin = SdfBuilder.getSimpleDateFormat().format(c.getTime());
        c.add(Calendar.DAY_OF_YEAR, 6);
        String end = SdfBuilder.getSimpleDateFormat().format(c.getTime());
           System.out.println(begin+" "+end);
        List<SumTime> sumTimesForWeek = sumTimeMapper.getSumTimesForWeek(begin, end);
        if (CollectionUtil.isEmpty(sumTimesForWeek)) {
            List<String> names = userService.getUserNames();
            for (String name : names) {
                SumTime sumTime = new SumTime();
                sumTime.setName(name);
                sumTime.setReturnTime(DateUtils.convertTimeToStr(0L));
                sumTimesForWeek.add(sumTime);
            }
            return sumTimesForWeek;
        }
        for (SumTime sumTime : sumTimesForWeek) {
            sumTime.setReturnTime(DateUtils.convertTimeToStr(sumTime.getSum()));
        }
        return sumTimesForWeek;
    }


    public List<SumTime> getTimeByUserId(Integer userId) {
        String year = SdfBuilder.getSimpleDateFormat().format(new Date()).split("-")[0];
        List<SumTime> lists = sumTimeMapper.getTimeByUserId(year+"-01-01",year+"-12-31",userId);
        for (SumTime list : lists) {
            list.setReturnTime(DateUtils.convertTimeToStr(Long.valueOf(list.getSumtime())));
        }
        return lists;
    }

    public List<ClockInRecord> getPersonalCIR(Integer userId, String date) throws Exception {
        String[] dates = DateUtils.getMonth(date);
        List<SumTime> sumTimes = sumTimeMapper.getPersonalCIR(userId, dates[0], dates[1]);
        List<ClockInRecord> res = new ArrayList<>();
        for (SumTime sumTime : sumTimes) {
            String ok = sumTime.getSumtime();
            String dt = sumTime.getDt();
            ClockInRecord re = new ClockInRecord(dt,ok.equals("0") ? 0 : 1);
            res.add(re);
        }
        return res;
    }

    public RespPageBean getYearMonthSumTime(Integer page, Integer size) {
        if(page!=null && page>0){
            page = (page - 1)*size;
        }
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setTotal(userMapper.getTotal(null,null,null,null,null));
        String[] yearMonth = DateUtils.getYearMonth();
        List<SumTimeVo> l1 = sumTimeMapper.getYearTime(yearMonth[0],yearMonth[1],page,size);
        List<SumTimeVo> l2 = sumTimeMapper.getMonthTime(yearMonth[2],yearMonth[3],page,size);
        List<SumTimeVo> ll = sumTimeMapper.getTotalTime(page,size);
        for (int i = 0; i < ll.size(); i++) {
            SumTimeVo sumTimeVototal = ll.get(i);
            SumTimeVo sumTimeVoyear = l1.get(i);
            SumTimeVo sumTimeVomonth = l2.get(i);
            sumTimeVototal.setReceivedYear(sumTimeVoyear.getReceivedYear());
            sumTimeVototal.setYearSumTime(DateUtils.convertTimeToStr(sumTimeVoyear.getReceivedYear()));
            sumTimeVototal.setReceivedMonth(sumTimeVomonth.getReceivedMonth());
            sumTimeVototal.setMonthSumTime(DateUtils.convertTimeToStr(sumTimeVomonth.getReceivedMonth()));
            sumTimeVototal.setTotalSumTime(DateUtils.convertTimeToStr(sumTimeVototal.getReceivedTotal()));
        }

        respPageBean.setData(ll);
        return respPageBean;
    }

    public int isExist(Integer id, String day) {
        return sumTimeMapper.isExist(id, day);
    }

    public void insertSumTime(Integer id, String day) {
        SumTime record = new SumTime();
        record.setUserid(id);
        record.setDt(day);
        record.setSumtime("0");
        sumTimeMapper.insertSelective(record);
    }

    public SumTime getSumTime(Integer userId, String day) {
        return sumTimeMapper.getSumTime(userId,day);
    }

    public void update(SumTime sumTime) {
        sumTimeMapper.updateByPrimaryKeySelective(sumTime);
    }

}
